package view;

import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.LayoutManager;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.util.Collections;
import java.util.List;

import org.havi.ui.event.HRcEvent;

import components.AbstractComponent;
import components.Device;
import components.Edit;
import components.Form;

import device.DeviceHelper;

public class Render {

    private Device device;

    public Render() {}

    public Render(Device device) {
        this.device = device;
    }

    public Button render(final components.Button com_Button, Container parent) {
        Button button = new Button();
        button.setLabel(com_Button.getText());
        button.setFont(new Font("Arial", Font.BOLD, 20));
        button.setForeground(Color.BLACK);
        button.setBackground(Color.GREEN);
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                DeviceHelper.getInstance().executeCommand(com_Button.getAction(), com_Button, device);
            }
        });
        parent.add(button);
        return button;
    }

    public Label render(components.Label com_Label, Container parent) {
        //Trabalhando com o label principal 
        Label label = new Label();
        label.setText(com_Label.getText());
        label.setFont(new Font("Arial", Font.BOLD, 15));
        label.setForeground(Color.BLACK);

        parent.add(label);
        return label;
    }

    public Panel render(components.Panel com_panel, Container parent) {
        parent.setLayout(new GridLayout(2, 2));
        return (Panel) parent;
    }

    public TextField render(final components.Edit edit, Container parent) {
        final TextField textField = new TextField();
        textField.addKeyListener(new KeyListener() {

            public void keyTyped(KeyEvent e) {}

            public void keyReleased(KeyEvent e) {}

            public void keyPressed(KeyEvent e) {
                String text = textField.getText();
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_DELETE:
                    case KeyEvent.VK_BACK_SPACE:
                        if (text.length() == 0) {
                            return;
                        }
                        if (textField.getSelectionEnd() > 0) {
                            String sub = text.substring(0, textField.getSelectionStart());
                            sub += text.substring(textField.getSelectionEnd(), text.length());
                            textField.setText(sub);
                        } else {
                            textField.setText(text.substring(0, text.length() - 1));
                        }
                        break;
                    default:
                        if (textField.getSelectionEnd() > 0) {
                            String sub = text.substring(0, textField.getSelectionStart());
                            sub += text.substring(textField.getSelectionEnd(), text.length());
                            text = sub;
                            textField.setText(text);
                        }
                        textField.setText(text + e.getKeyChar());
                        break;
                }
                edit.setValue(textField.getText());
            }
        });
        parent.add(textField);
        return textField;
    }

    public Button getButton(String text, Container parent, ActionListener actionListener) {
        Button button = new Button();
        //button.setSize(90, 40);
        button.setLabel(text);
        //button.setFont(new Font("Arial", Font.BOLD, 20));
        //button.setForeground(Color.BLACK);
        //button.setBackground(Color.GREEN);
        button.addActionListener(actionListener);
        //button.setLocation((panel.getWidth() / 3) + 30, 150);
        parent.add(button);
        return button;
    }

    public Choice getCombo(List<String> values, Container parent) {
        if (values == null) {
            values = Collections.<String> emptyList();
        }
        Choice choice = new Choice();
        for (String value : values) {
            choice.addItem(value);
        }
        parent.add(choice);
        return choice;
    }

    public java.awt.List getComboList(List<String> values, Container parent, Object constrains) {
        if (values == null) {
            values = Collections.<String> emptyList();
        }
        final java.awt.List choice = new java.awt.List();
        for (String value : values) {
            choice.add(value);
        }
        if (!values.isEmpty()) {
            choice.select(0);
        }
        choice.addKeyListener(new KeyListener() {

            public void keyTyped(KeyEvent e) {}

            public void keyReleased(KeyEvent e) {}

            public void keyPressed(KeyEvent e) {
                if (e.getKeyChar() == HRcEvent.VK_UP) {
                    int index = choice.getSelectedIndex() - 1;
                    if (index != -1) {
                        choice.select(index);
                    }
                }
                if (e.getKeyChar() == HRcEvent.VK_DOWN) {
                    int index = choice.getSelectedIndex() + 1;
                    if (index <= choice.getItemCount()) {
                        choice.select(index);
                    }
                }
            }
        });
        parent.add(choice, constrains);
        return choice;
    }

    public Label getLabel(String text, Container parent) {
        Label label = new Label();
        label.setText(text);
        //label1.setLocation((panel.getWidth() / 3) + 30, 5);
        label.setFont(new Font("Arial", Font.BOLD, 30));
        // label1.setAlignment(Label.CENTER);
        //label1.setBackground(Color.BLACK);
        label.setForeground(Color.BLACK);
        //label1.setSize(18 * text.length(), 40);
        parent.add(label);
        return label;
    }

    public Label getLabel(String text, int aligment, Container parent, Object constrains) {
        Label label = new Label();
        label.setText(text);
        label.setAlignment(aligment);
        parent.add(label, constrains);
        return label;
    }

    public Panel getPanel(LayoutManager layoutManager, Container parent) {
        Panel panel = new Panel();
        panel.setBackground(Color.RED);
        //panel.setLocation(115, 45);
        panel.setLayout(layoutManager);
        parent.add(panel);
        return panel;
    }

    public TextField getTextField(Container parent, Object constrains) {
        final TextField textField = new TextField();
        textField.addTextListener(new TextListener() {

            public void textValueChanged(TextEvent e) {
            //System.out.println("oii");
            }
        });
        textField.addKeyListener(new KeyListener() {

            public void keyTyped(KeyEvent e) {
            //System.out.println("oi keyTyped: " + e.getKeyChar());
            }

            public void keyReleased(KeyEvent e) {
            //System.out.println("oi keyReleased:" + e.getKeyChar() + " keyCode: " + e.getKeyCode());
            }

            public void keyPressed(KeyEvent e) {
                //System.out.println("oi keyPressed: " + e.getKeyChar() + " keyCode: " + e.getKeyCode());
                String text = textField.getText();
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_DELETE:
                    case KeyEvent.VK_BACK_SPACE:
                        if (text.length() == 0) {
                            return;
                        }
                        if (textField.getSelectionEnd() > 0) {
                            String sub = text.substring(0, textField.getSelectionStart());
                            sub += text.substring(textField.getSelectionEnd(), text.length());
                            textField.setText(sub);
                        } else {
                            textField.setText(text.substring(0, text.length() - 1));
                        }
                        break;
                    default:
                        if (textField.getSelectionEnd() > 0) {
                            String sub = text.substring(0, textField.getSelectionStart());
                            sub += text.substring(textField.getSelectionEnd(), text.length());
                            text = sub;
                            textField.setText(text);
                        }
                        textField.setText(text + e.getKeyChar());
                        break;
                }
            }
        });
        parent.add(textField, constrains);
        return textField;
    }

    public GridBagConstraints createConstrains(int fill, int gridy, int gridx, double weightx, int anchor) {
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        if (fill > 0) {
            gridBagConstraints.fill = fill;
        }
        gridBagConstraints.gridy = gridy;
        gridBagConstraints.gridx = gridx;
        if (weightx > 0) {
            gridBagConstraints.weightx = weightx;
        }
        if (anchor > 0) {
            gridBagConstraints.anchor = anchor;
        }
        return gridBagConstraints;
    }

    public GridBagConstraints createConstrains(int gridx, int gridy) {
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = gridx;
        gridBagConstraints.gridy = gridy;
        return gridBagConstraints;
    }

    public GridBagConstraints createConstrains(int fill, int gridy, int gridx, int anchor, double weightx, double weighty, int gridwidth, int gridheight) {
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = fill;
        gridBagConstraints.gridy = gridy;
        gridBagConstraints.anchor = anchor;
        gridBagConstraints.weightx = weightx;
        if (weighty > 0) {
            gridBagConstraints.weighty = weighty;
        }
        if (gridwidth > 0) {
            gridBagConstraints.gridwidth = gridwidth;
        }
        if (gridheight > 0) {
            gridBagConstraints.gridheight = gridheight;
        }
        gridBagConstraints.gridx = gridx;
        return gridBagConstraints;
    }

    public Container renderForm(Form form) {
        Panel panel = new Panel();
        if (form.getHeight() != null) {
            int width = Integer.valueOf(form.getWidth());
            int height = Integer.valueOf(form.getHeight());
            panel.setSize(width, height);
        } else {
            panel.setSize(430, 250);
        }
        panel.setBackground(Color.WHITE);
        panel.setLocation(135, 85);
        panel.setLayout(new FlowLayout());

        renderChindrens(form.getChildrens(), panel);

        return panel;

    }

    private void renderChindrens(List<AbstractComponent> list, Container parent) {
        for (AbstractComponent component : list) {
            if (component instanceof components.Button) {
                render((components.Button) component, parent);
            } else if (component instanceof components.Panel) {
                render(((components.Panel) component), parent);
            } else if (component instanceof components.Label) {
                render(((components.Label) component), parent);
            } else if (component instanceof Edit) {
                render((components.Edit) component, parent);
            }

        }
    }

}
